/*
 * sc_p.c
 *
 * Created: 2012-07-09 07:09:24
 *  Author: Antrykot
 */ 

#define P_VER	0x0004

#include "sc-p-ps.h"   
#include "../sc-p/utils.h"
#include "../sc-p/trans-p.h"
#include "measure.h"
                      
 
ISR(TIMER0_OVF_vect)
{
	t0_cnt ++;
	
	if (t0_cnt > 15)	// 30ms (256us * 8div * 15)
	{
		t0_cnt = 0;
		timeout = 1;
		T0STOP;
		PTX_H; 
	}
}


ISR(INT0_vect)
{
	LG_ON;
	trans();
	WAITMS(10);
	LG_OFF;
	PTX_H;
	EIFR |= (1 << INTF0);
}




int main(void)
{



	LR_ON;
	setup_io();
	
	WAITMS(30);		
	init_eeprom();
	DCDC_ON;
	WAITMS(300);
	
	LY_ON;
		
	sei();
	TR_P_ENABLE;
		
	PTX_H;
	LG_ON;
		
    while(1)
    {
	
#ifdef MEDIA_PS
	
	calc_pa();
	calc_t();
	WAITMS(290);
	calc_rh();
#elif MEDIA_PD 
	calc_pd();
#endif
		
		LY_ON;
		
		calc_aku();
		calc_pow();
		calc_dc(); 

		LY_OFF;
		 
		WAITMS(50);

    }
}



void setup_io()
{
	
	PORTA = 0;
	DDRA = 0;

	DDRD = 0 | ( 1 << 3);	// DC/DC on off

	PORTB = 0;
	
	DDRB = 0 | ( 1 << 0);	// DC/DC 5/100%
	DDRB |= ( 1 << 1);		// ADC CLK
	DDRB &= ~( 1 << 2);		// ADC DOUT
	DDRB |= ( 1 << 3);		// ADC DIN
	DDRB |= ( 1 << 4);		// ADC CS
	
	DDRB |= ( 1 << 5);		// YELLOW
	DDRB |= ( 1 << 6);		// RED
	DDRB |= ( 1 << 7);		// GREEN
	
	PORTD = 0;
	
	DDRD |=  (1 << 1);			// P_TX
	DDRD &= ~(1 << 0);			// P_RX
	PORTD |= (1 << 0) | (1 << 2);			// RX set high, CLK set high
	
	EICRA = 0 | (1 << ISC01);		// INT0 & INT1 falling edge

	
	//TR_K_ENABLE;
	//TR_P_ENABLE;
		
	TCCR0B = 0 | ( 1 << CS01);		// T0, prescale 8
	TIMSK0 |= ( 1 << TOIE0);		// interrupt T0 overflow


	
}
#ifdef MEDIA_PD
#define DEF_EEPFLAG 0xA1
#elif MEDIA_PS
#define DEF_EEPFLAG 0xA2
#endif

void init_eeprom()
{
	u8 x = _read(0);
	
	if (x != DEF_EEPFLAG)
	{
#ifdef MEDIA_PS
		write_double(EEP_PRESA,3.76);
		write_double(EEP_PRESB,-40);
#elif MEDIA_PD
		write_double(EEP_PRESA,0.27);
		write_double(EEP_PRESB,1);
#endif
		write_word(EEP_PRESZ,1000);
		write_word(EEP_PRESM,2800);
		

		write_word(EEP_X_AKU,STD_AKU_MINUS);
		write_word(EEP_X_POW,STD_AKU_MINUS);
		_write(0,DEF_EEPFLAG);
		
	}
 
	x_aku = read_word(EEP_X_AKU);
	x_pow = read_word(EEP_X_POW);
	
	ap_a = read_double(EEP_PRESA);
	ap_b = read_double(EEP_PRESB);
		
	

}



////////// GLOBAL

u16 eep_cnt = 0;
u16 timer_cnt = 0;
u16 timer_cnt_h = 0;

float m1,m2,m3,m4,m5,m6,m7,m8,m9;
u16 x0,x1,x2;
float w1,w2,w3,w4,w5;
float w_det;

double swxa;
double swxb;
double swxc;

double wxa;
double wxb;
double wxc;

double snxa;
double snxb;

double nxa;
double nxb;

u16 x_szw;
u16 x_scw;

u16 x_zw;
u16 x_cw;

u16 x_szn;
u16 x_scn;

u16 x_zn;
u16 x_cn;

u8 t_addr;
u16 t_data;
u16 t_pom;
u8 t_crc;

u8 r_addr;
u16 r_data;
u8 r_crc;

u8 tr_ok = 0;

u8 crc_data[10];
u8 crc_data_len;

#ifdef MEDIA_PS

float ap_a;
float ap_b;

#endif

#ifdef MEDIA_PD

float ap_a;
float ap_b;

#endif
